//
//  TestViewController.m
//  leo
//
//  Created by 常艳 on 15/9/15.
//  Copyright (c) 2015年 mdx. All rights reserved.
//

#import "TestViewController.h"
#import "HttpRequester.h"
#import "SyncManager.h"
#import "DataManager.h"

#import "MainPageViewController.h"
#import "LocationViewController.h"
#import "GrowthViewController.h"
#import "ManagerViewController.h"
#import "ChatListViewController.h"
#import "ChooseRelationViewController.h"
@interface TestViewController ()
@property(strong,nonatomic) HttpRequester *theRequester;
@property (nonatomic,strong) SyncManager *syncManager;
@property (nonatomic,strong) DataManager *dataManager;
@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.theRequester=[[HttpRequester alloc] init];
    self.syncManager=[[SyncManager alloc] init];
    self.dataManager=[[DataManager alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)mainEnter:(id)sender {
    NSMutableArray *items = [[NSMutableArray alloc]init];
    
    LocationViewController *locationViewController = [[LocationViewController alloc]init];

    //[items addObject:locationViewController];
    [items addObject:locationViewController];
    
    ChatListViewController *chatViewController = [[ChatListViewController alloc]init];
    UINavigationController *tempNavLocation = [[UINavigationController alloc]initWithRootViewController:chatViewController];
    [items addObject:tempNavLocation];
    
//Test
//    GrowCenterViewController *growViewController = [[GrowCenterViewController alloc]init];
//    [items addObject:growViewController];
    GrowthViewController *growthViewController = [[GrowthViewController alloc]init];
    UINavigationController *tempNavGrowth = [[UINavigationController alloc]initWithRootViewController:growthViewController];
    [items addObject:tempNavGrowth];
    
    ManagerViewController *managerViewController = [[ManagerViewController alloc]init];
    UINavigationController *tempNavManager = [[UINavigationController alloc]initWithRootViewController:managerViewController];
    //[items addObject:managerViewController];
    [items addObject:tempNavManager];
    MainPageViewController *mainPageViewController = [[MainPageViewController alloc]init];
    
    [mainPageViewController setTitle:@"TabbarViewController"];
    [mainPageViewController setViewControllers:items];
    [self presentViewController:mainPageViewController animated:YES completion:nil];
}


- (IBAction)test:(id)sender {
//        [self.syncManager getCheckCode:@"15296661636" type:0 completion:^(SyncResult result,NSDictionary *retValue,NSString *description) {
//            [self callback:result retValue:retValue description:description];
//        }];     //手机请求SMS验证码,ok
    
    
    //        [self.syncManager appUpdateFinish:^(SyncResult result) {
    //            [self callback:result];
    //        }];//手机app升级完成
    
    ChooseRelationViewController *relation=[[ChooseRelationViewController alloc] init];
    [self.navigationController pushViewController:relation animated:YES];
    
}


- (IBAction)verifySms:(id)sender {
    
            [self.syncManager verifySmsCode:@"15296661636" smsCode:@"123456" completion:^(SyncResult result,NSDictionary *retValue,NSString *description) {
                [self callback:result retValue:retValue description:description];
            }];//手机请求确认SMS验证码,ok
}
- (IBAction)registerPhone:(id)sender {
    [self.syncManager registerPhone:@"15296661636" pass:@"123456" nickname:@"yan" completion:^(SyncResult result,NSDictionary *retValue,NSString *description) {
                [self callback:result retValue:retValue description:description];
    
            }];//注册手机,ok
}
- (IBAction)loginPhone:(id)sender {
//    [self.syncManager loginPhone:@"15296668009" pass:@"123456" completion:^(SyncResult result, NSDictionary *retValue,NSString *description) {
//        [self callback:result retValue:retValue description:description];
//    }]; // 手机登录 ok
}
- (IBAction)resetPwd:(id)sender {
    [self.syncManager resetPwd:@"15296661636" password:@"111" smsCode:@"123456" completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
                [self callback:result retValue:retValue description:description];
            }];//用户忘记密码  OK
}
- (IBAction)bind:(id)sender {
    [self.syncManager bindDevice:@"9" bindCode:@"12345678"  alias:@"爸爸" completion:^(SyncResult result, NSDictionary *retValue,NSString *description) {
            [self callback:result retValue:retValue description:description];
        }]; //手机申请绑定手表 ok
}
- (IBAction)unbind:(id)sender {
        /*[self.syncManager unbindDevice:@"67" completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
            [self callback:result retValue:retValue description:description];
        }];//解除绑定 ok*/
}
- (IBAction)confirm:(id)sender {
  //主管理员确认绑定  ok
}
- (IBAction)getAllWatch:(id)sender {
        [self.syncManager getBoundDevices:^(SyncResult result, NSDictionary *retValue, NSString *description) {
            [self callback:result retValue:retValue description:description];
        }];//获取所有手表 ok
}
- (IBAction)deleteViceAdmin:(id)sender {
        [self.syncManager deleteViceAdmin:@"13" viceAdminUid:@"18" completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
             [self callback:result retValue:retValue description:description];
        }];//主管理员删除副管理员
}
- (IBAction)getAllManager:(id)sender {
        [self.syncManager getBoundUsers:@"8" completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
            [self callback:result retValue:retValue description:description];
        }];//获取绑定的所有管理员 ok
}

- (IBAction)getAllContact:(id)sender {
        [self.syncManager getWatchContact:@"8" completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
            [self callback:result retValue:retValue description:description];
        }];//APP获取手表所有联系人
}
- (IBAction)setLocatePara:(id)sender {
    [self.syncManager setLocateParameters:@"13" reportInterval:10 sampleInterval:10 completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        [self callback:result retValue:retValue description:description];//设置定位参数
    }];
}
- (IBAction)getLocatePara:(id)sender {
    [self.syncManager getLocateParameters:@"13" completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        [self callback:result retValue:retValue description:description];
    }];//获取定位参数
}
- (IBAction)addGeoFence:(id)sender {
    Fence *fen1=[[Fence alloc] init];
    fen1.deviceUid=@"8";
    fen1.latitude=52.1;
    fen1.longtitude=126.1;
    fen1.altitude=152;
    fen1.desc=@"school";
    fen1.radius=10;
    fen1.title=@"a";
    fen1.start_time=@"09:00";
    fen1.end_time=@"12:00";
    fen1.cycle=@"1,2,3,4";
    fen1.is_active=1;
    fen1.type=1;
    [self.syncManager addGeoFence:@"8" fence:fen1 completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        
    }];
    
}

- (IBAction)setGeoFence:(id)sender {
    
    Fence *fen2=[[Fence alloc] init];
    fen2.deviceUid=@"8";
    fen2.latitude=77.0;
    fen2.longtitude=137.0;
    fen2.altitude=111;
    fen2.desc=@"home";
    fen2.radius=15;
    fen2.title=@"h";
    fen2.start_time=@"09:00";
    fen2.end_time=@"13:00";
    fen2.cycle=@"1,2,3,4";
    fen2.is_active=1;
    fen2.fence_id=@"3";
    
   [self.syncManager setGeoFence:@"8" fence:fen2 completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
       
   }];
}
- (IBAction)getGeoFence:(id)sender {
    [self.syncManager getGeoFences:@"8" completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        [self callback:result retValue:retValue description:description];
    }];//获取地理围栏
}
- (IBAction)deleteGeoFence:(id)sender {
    [self.syncManager deleteGeoFence:@"8" fenceId:@"3" completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        [self callback:result retValue:retValue description:description];
    }];//删除地理围栏
}
- (IBAction)getMotonTrack:(id)sender {
    [self.syncManager getMotionTrack:@"8" withStartTime:@"null" andEndTime:@"null" completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        [self callback:result retValue:retValue description:description];
    }];//获取运动轨迹
}
- (IBAction)getLatestPos:(id)sender {
    [self.syncManager getLatestPosition:@"8" completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        [self callback:result retValue:retValue description:description];
    }];// 获取最新定位信息
}
- (IBAction)immediaLoc:(id)sender {
    [self.syncManager locateImmediately:@"8" completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        [self callback:result retValue:retValue description:description];
    }];// 手表立即定位
}
- (IBAction)addContact:(id)sender {
   
    Contact *contact1=[[Contact alloc] init];
    contact1.phoneNum=@"15296661006";
    contact1.contactname=@"hhh";
    contact1.type=1;
    contact1.photoName=@"toy_L5_unlock@2x.png";
    contact1.groupId=7;
    contact1.photo_url=[[NSBundle mainBundle]  pathForResource:@"toy_L5_unlock@2x" ofType:@"png"];
    contact1.deviceUid=@"8";
    
    
    [self.syncManager addWatchContact:contact1 completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        [self callback:result retValue:retValue description:description];
    }];

    
}

- (IBAction)deleteContact:(id)sender {
    [self.syncManager deleteContact:@"8" contactId:@"4" completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        [self callback:result retValue:retValue description:description];
    }];
}

- (IBAction)setContact:(id)sender {
    Contact *contact1=[[Contact alloc] init];
    contact1.phoneNum=@"15296661009";
    contact1.contactname=@"mama";
    contact1.type=1;
    contact1.photoName=@"toy_L5_unlock@2x.png";
    contact1.groupId=7;
    contact1.user_id=@"8";
    contact1.photo_url=[[NSBundle mainBundle]  pathForResource:@"toy_L5_unlock@2x" ofType:@"png"];
    contact1.deviceUid=@"8";
    [self.syncManager setWatchContact:contact1 completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        [self callback:result retValue:retValue description:description];
    }];
}

- (IBAction)setUser:(id)sender {
    
    User *user=[User sharedSingleton];
    user.email=@"315261347@qq.com";
    user.birth=@"1985-04-03";
    user.age=30;
    user.address=@"jincheng";
    user.nickName=@"yan";
    user.pic_url=[[NSBundle bundleForClass:[self class]] pathForResource:@"avater_boy" ofType:@"png"];
    user.photoName=@"avater_boy.png";
    [self.syncManager setUserInfo:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        
    }];
    
}
- (IBAction)setWatch:(id)sender {
    Watch *watch=[[Watch alloc] init];
    watch.deviceUid=@"8";
    watch.nickname=@"you";
    watch.height=20;
    watch.weight=74;
    watch.age=1;
    watch.birth=@"2014-09-30 06-38-00";
    watch.gender=1;
    watch.className=@"1";
    watch.pic_url=[[NSBundle bundleForClass:[self class]] pathForResource:@"avater_boy" ofType:@"png"];
    watch.photoName=@"avater_boy.png";
    [self.syncManager setWatchInfo:watch completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        
    }];
}
- (IBAction)uploadGroupChat:(id)sender {
    Chat *chat=[[Chat alloc] init];
    chat.groupId=6;
    chat.genTime=[Tooler getTS];
    chat.createTime=[Tooler getCurrentTime];
    chat.duration=10;
    chat.user_id=@"10";
    chat.accountId=@"10";
    chat.file_name=@"466853267373.caf";
    chat.file_url=[[Tooler getDocumentDirectory] stringByAppendingString:@"/record/466853267373.caf"];
    chat.type=0;
    [self.syncManager uploadGroupChatRecord:chat completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        [self callback:result retValue:retValue description:description];
    }];
    
}
- (IBAction)uploadSingleChat:(id)sender {
    Chat *chat=[[Chat alloc] init];
    chat.genTime=[Tooler getCurrentTime];
    chat.createTime=[Tooler getCurrentTime];
    chat.duration=10;
    chat.user_id=@"10";
    chat.accountId=@"10";
    chat.file_name=@"466853267373.caf";
    chat.file_url=[[Tooler getDocumentDirectory] stringByAppendingString:@"/record/466853267373.caf"];
    chat.type=0;
    chat.targetUid=@"8";
    [self.syncManager uploadSingleChatRecord:chat completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        
    }];
}
- (IBAction)getGroupChat:(id)sender {
    [self.syncManager getGroupChatRecords:6 seqIdBegin:@"10" limit:2 deviceUid:@"8" completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        
    }];
    
}
- (IBAction)getSingleChat:(id)sender {
    [self.syncManager getSingleChatRecords:@"8" seqIdBegin:@"10" limit:10 deviceUid:@"8" completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        
    }];
}

- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)callback:(SyncResult) result retValue:(NSDictionary *)ret description:(NSString *)description
{
    
    if(result==SUCESS)
    {
        NSLog(@"sync result is sucess");
    }
    else
    {
        NSLog(@"sync fail,error is %@",description);
    }
}
- (IBAction)testDataAPI:(id)sender {
    User *user=[User sharedSingleton];
    
    //获取管理员列表
    [self.dataManager getManagerLists:@"3" accountId:user.uid];//ok
    //获取联系人列表
   [self.dataManager getContactLists:@"3" accountId:user.uid];//ok
    //获取SOS联系人列表
    [self.dataManager getSOSContactLists:@"3" accountId:user.uid];//ok
    
    //获取指定管理员详细
    [self.dataManager getManager:@"1"];//ok
    //获取指定联系人详细信息
    [self.dataManager getContact:@"3"];//ok
    //获取绑定的所有手表列表
    [self.dataManager getWatchList:user.uid];//ok
    //获取指定手表的详细信息
    [self.dataManager getWatch:@"3"];//ok
    //获取安全区域列表
    [self.dataManager getFenceList:@"1"];//ok
    //获取指定定安全区域详细信息
    [self.dataManager getFence:@"2"];//ok
    //获取运动轨迹
    [self.dataManager getLocationList:@"3" startTime:@"1443454554000" endTime: @"1443455019001"];//ok
    //获取指定手表最新位置信息
    [self.dataManager getLatestLocation:@"3"];//ok
    //获取app当前版本信息
    [Tooler getAppVersion];//ok
}


@end
